# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql

class JdPipeline:
    # def open_spider(self,spider):
    #     self.file = open('111.txt','a')
    # def close_spider(self,spider):
    #     self.file.close()
    # def process_item(self, item, spider):
    #     con = str(item)+'\n'
    #     self.file.write(con)
    #     return item
    def open_spider(self,spider):
        self.con = pymysql.connect(host='10.30.59.136',port=3306,user='root',password='123456',database='wdr',charset='utf8')

        self.file = open('jd.txt','a')
    def close_spider(self,spider):
        self.file.close()
        self.con.close()
    def process_item(self, item, spider):
        self.cur = self.con.cursor()
        self.cur.execute('insert into jd_bjb (name,memory,cpu,storage,size,model,jiage,uid) values(%s,%s,%s,%s,%s,%s,%s,%s)',[item['name'],item['memory'],item['cpu'],item['storage'],item['size'],item['model'],item['jiage'],item['uid']])
        self.cur = self.con.commit()
       # self.cur.execute('insert into jd_bjb (name,memory,cpu,storage,size,model,jiage,uid) values(%s,%s,%s,%s,%s,%s,%s,%s)',[item['name'],item['memory'],item['cpu'],item['storage'],item['size'],item['model'],item['jiage'],item['uid']])
       # self.cur.execute('insert into jd_bjb (name,memory,cpu,storage,size,model,jiage,uid) values(%s,%s,%s,%s,%s,%s,%s,%s)',[item['name'],item['memory'],item['cpu'],item['storage'],item['size'],item['model'],item['jiage'],item['uid']])
       # self.cur = self.con.commit()
        connect = str.format('{0},{1},{2},{3},{4},{5},{6},{7}\n',item['name'],item['memory'],item['cpu'],item['storage'],item['size'],item['model'],item['jiage'],item['uid'])

        self.file.write(connect)
        return item
